﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using Hxsoft.AppV1;

namespace Hxsoft.AppV1.Module
{
	/// <summary>
	/// ChangePasswordRecords 类。
	/// </summary>
	public static partial class ChangePasswordRecords
	{
		public const string INFO_TYPE = "ChangePasswordRecord";		// 信息的类别代码


		#region GetItem
		/// <summary>
		/// 根据标识获取指定的ChangePasswordRecords。
		/// </summary>
		/// <param name="o">ChangePasswordRecords集合。</param>
		/// <param name="userId">获取的ChangePasswordRecords标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static ChangePasswordRecord GetItem(this System.Data.Linq.Table<ChangePasswordRecord> o, Guid userId)
		{
			return o.SingleOrDefault(p => p.UserId == userId);
		}

		/// <summary>
		/// 根据标识获取指定的ChangePasswordRecords。
		/// </summary>
		/// <param name="userId">获取的ChangePasswordRecords标识 UserId。</param>
		/// <returns>获取的信息项。</returns>
		public static ChangePasswordRecord GetItem(Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ChangePasswordRecords.GetItem(userId);
		}
		#endregion



		#region Exists
		/// <summary>
		/// 验证指定的ChangePasswordRecords是否已经存在。
		/// </summary>
		/// <param name="o">ChangePasswordRecords集合。</param>
		/// <param name="userId">验证的ChangePasswordRecords标识 UserId。</param>
		/// <returns>布尔值表示指定的ChangePasswordRecords是否已经存在。</returns>
		public static bool Exists(this System.Data.Linq.Table<ChangePasswordRecord> o, Guid userId)
		{
			return o.Any(p => p.UserId == userId);
		}

		/// <summary>
		/// 验证指定的ChangePasswordRecords是否已经存在。
		/// </summary>
		/// <param name="userId">验证的ChangePasswordRecords标识 UserId。</param>
		/// <returns>布尔值表示指定的ChangePasswordRecords是否已经存在。</returns>
		public static bool Exists(Guid? userId)
		{
			if (!userId.HasValue) return false;
			GenericModuleDataContext dc = new GenericModuleDataContext();
			dc.ObjectTrackingEnabled = false;
			return dc.ChangePasswordRecords.Exists(userId.Value);
		}
		#endregion
	}



	/// <summary>
	/// ChangePasswordRecords 类操作部分。
	/// </summary>
	public static partial class ChangePasswordRecords
	{
		#region Insert
		/// <summary>
		/// 添加ChangePasswordRecords。
		/// </summary>
		/// <param name="insertedChangePasswordRecord">添加的ChangePasswordRecords。</param>
		/// <returns>添加的ChangePasswordRecords。</returns>
		public static ChangePasswordRecord Insert(ChangePasswordRecord insertedChangePasswordRecord)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ChangePasswordRecord changePasswordRecord = insertedChangePasswordRecord.ToNewEntity<ChangePasswordRecord>();			
			changePasswordRecord.ChangeDate = DateTime.Now;	
			
			dc.ChangePasswordRecords.InsertOnSubmit(changePasswordRecord);
			dc.SubmitChanges();

			// 返回只读数据
			return insertedChangePasswordRecord.FillByEntity(changePasswordRecord);
		}
		#endregion



		#region Update
		private static readonly HashSet<string> _updatedExcludeColumns = typeof(ChangePasswordRecord).GetEntityColumnNames(new string[] {
				"UserId"	// 在这里填写更新的排除列
			}, true);

		/// <summary>
		/// 修改ChangePasswordRecords。
		/// </summary>
		/// <param name="updatedChangePasswordRecord">修改的ChangePasswordRecords。</param>
		/// <returns>修改的ChangePasswordRecords。</returns>
		public static ChangePasswordRecord Update(ChangePasswordRecord updatedChangePasswordRecord)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ChangePasswordRecord changePasswordRecord = dc.ChangePasswordRecords.GetItem(updatedChangePasswordRecord.UserId);
			if (changePasswordRecord == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);			

			changePasswordRecord.FillByEntity(updatedChangePasswordRecord, EntityColumnsSetType.Exclude, _updatedExcludeColumns);

			try
			{
				dc.SubmitChanges(ConflictMode.ContinueOnConflict);
			}
			catch (ChangeConflictException e)
			{
				foreach (ObjectChangeConflict occ in dc.ChangeConflicts)
				{
					occ.Resolve(RefreshMode.KeepCurrentValues);
				}
			}
			
			// 返回只读数据
			return updatedChangePasswordRecord.FillByEntity(changePasswordRecord);
		}
		#endregion

		#region Delete
		/// <summary>
		/// 删除ChangePasswordRecords。
		/// </summary>
		/// <param name="userId">删除的ChangePasswordRecords标识 UserId。</param>
		public static void Delete(Guid userId)
		{
			GenericModuleDataContext dc = new GenericModuleDataContext();

			ChangePasswordRecord changePasswordRecord = dc.ChangePasswordRecords.GetItem(userId);
			if (changePasswordRecord == null)
				throw new ObjectNotExistsException(Res.Operation_ObjectNotExists);			

			Delete(dc, changePasswordRecord);

			dc.SubmitChanges();
		}

		#region internal Delete
		internal static void Delete(GenericModuleDataContext dc, ChangePasswordRecord changePasswordRecord)
		{
			dc.ChangePasswordRecords.DeleteOnSubmit(changePasswordRecord);
		}

		internal static void DeleteByParent(GenericModuleDataContext dc, User parent)
		{
            if(Exists(parent.UserId))
                Delete(parent.UserId);
		}
		#endregion
		#endregion
	}
}